<?
function drizzo_get_the_comments($post_id = null, $comment_parent = 0, $level = 0, $sort_order = "DESC") {
	global $drizzo_users, $drizzo_comments, $the_post;
		
	drizzo_db_connect();
	
	$comment_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `post_id`='$post_id' AND `status`='approved' AND `parent_id`='$comment_parent' ORDER BY `ID` $sort_order");
	
	if($comment_query) {

		while($row = mysql_fetch_array($comment_query)) {
			$comment_id = $row['ID'];
			$title = html_entity_decode(stripslashes($row['title']));
			$content = html_entity_decode(stripslashes($row['content']));
			$author_id = $row['author_id'];
			$author_name = $row['author_name'];
			$author_url = $row['author_url'];
			$author_email = $row['author_email'];
			$date = $row['date'];
			$parent_id = $row['parent_id'];
			
			if($author_id != 0) {
				$author_lookup = mysql_query("SELECT * FROM `$drizzo_users` WHERE `ID`='$author_id' LIMIT 1");
				if($author_lookup) {
					$author_array = mysql_fetch_array($author_lookup);
					$author_name = $author_array['nice_name'];
					if($author_name == null) {
						$author_name = $author_array['username'];
					}
					$author_url = $author_array['url'];
					$author_email = $author_array['email'];
				}
			}
			
			if($parent_id == $comment_parent) {
				$the_comments_array[] = array(
					"ID" => $comment_id,
					"title" => $title,
					"content" => $content,
					"author_id" => $author_id,
					"author" => $author_name,
					"author_url" => $author_url,
					"author_email" => $author_email,
					"date" => $date,
					"level" => $level,
					"parent_id" => $parent_id
					);
					
					drizzo_get_the_comments($post_id, $comment_id, $level +1);
			}
		}

		return $the_comments_array;

	} else {
		return false;
	}
}

function drizzo_get_the_comment($comment_id = null) {
	global $drizzo_comments, $drizzo_posts;
		
	drizzo_db_connect();
	
	$comment_query = mysql_query("SELECT * FROM `$drizzo_comments` WHERE `ID`='$comment_id'");
	if($comment_query) {
		while($row = mysql_fetch_array($comment_query)) {
			$title_query = @mysql_query("SELECT `title` FROM `$drizzo_posts` WHERE `ID`='$row[post_id]'");
			if($title_query) {
				$post_title = mysql_fetch_array($title_query);
			}
			$the_comments_array = array(
				"ID" => $row['ID'],
				"title" => html_entity_decode(stripslashes($row['title'])),
				"content" => html_entity_decode(stripslashes($row['content'])),
				"author_id" => $row['author_id'],
				"author" => stripslashes($row['author_name']),
				"author_url" => stripslashes($row['author_url']),
				"author_email" => html_entity_decode(stripslashes($row['author_email'])),
				"status" => $row['status'],
				"tags" => $row['tags'],
				"date" => $row['date'],
				"post_title" => $post_title['title']
				);
		}
		return $the_comments_array;
	} else {
		return false;
	}
}

function drizzo_the_comments($post_id = null, $style = "ol", $ajax = false) {
	global $the_post;
	
	if($the_post) {
		$post_id = $the_post['ID'];
	}
	$no = 0;
	$comments = drizzo_get_the_comments($post_id);

	if($comments) {
		if($style == "ul" || $style == "ol") { // Comments are a standard list
			if($style == "ul") {
				$comments_string .= "<ul id=\"comments_list\">\n";
			}

			if($style == "ol") {
				$comments_string .= "<ol id=\"comments_list\">\n";
			}

			foreach($comments as $comment) {
				$level = $comment['level'];
				if($the_post['author'] == $comment['author_id']) {
					$comments_string .= "\t<li class=\"comment author\" style=\"padding-left: " . $level . "00%;\"><a name=\"comment-" . $comment['ID'] . "\"></a>";
				} else {
					$comments_string .= "\t<li class=\"comment\" style=\"padding-left: " . $level . "00%;\"><a name=\"comment-" . $comment['ID'] . "\"></a>";
				}
				$gravatar_url = "http://www.gravatar.com/avatar.php?gravatar_id=" . $comment['author_email'];
				$user_avatar_url = "http://" . domain_name . admin_folder . "engine/user_avatar.php?user_id=" . $comment['author_id'];
				$comments_string .= "<span class=\"avatar\">";
				if($comment['author_url']) {
					$comments_string .= "<a href=\"" . $comment['author_url'] . "\">";
				}
				if(file_exists($gravatar_url)) {
					$comments_string .= "<img src=\"" . $gravatar_url . "\" alt=\"Gravatar\" class=\"avatar_img\" /><br />";
				} elseif($comment['author_id'] > 0) {
					$comments_string .= "<img src=\"" . $user_avatar_url . "\" alt=\"Avatar\" class=\"avatar_img\" width=\"40\" height=\"40\" /><br />";
				} else {
					// Do Nothing
				}
				$comments_string .= $comment['author'];
				if($comment['author_url']) {
					$comments_string .= "</a>";
				}
				$comments_string .= "</span>";
				if($comment['title'] != null) {
					$comments_string .= "<h2 class=\"comment_title\">" . $comment['title'] . "</h2>\n";
				}
				$comments_string .= "<div class=\"comment_content\">" . $comment['content'] . "</div>";
				$comments_string .= "<span class=\"comment_date\">Posted on " . date("M d, Y, g:i A", $comment['date']) . "</span>";
				$comments_string .= "<br class=\"clear\" /></li>\n";
			}
			if($style == "ul") {
				$comments_string .= "</ul>\n";
			}

			if($style == "ol") {
				$comments_string .= "</ol>\n";
			}
		}
	} else {
		$comments_string = false;
	}
	if(!$_POST) {
		if($ajax) {
			$comments_string .= "<h2><a href=\"javascript:void(0)\" onclick=\"new Ajax.Updater('comment_box', '" . drizzo_theme('directory') . "comment_box.php?ajax=true&post_id=" . $post_id . "', {onComplete:function(){ new Effect.Appear('comment_box');},asynchronous:true, evalScripts:true})\" title=\"Add a Comment\">Add a Comment</a></h2>\n";
			$comments_string .= "<div id=\"comment_box\" style=\"display: none;\"></div>\n";
		} else {
			$comments_string .= "<h2>Add a Comment</h2>\n";
			$comments_string .= "<div id=\"comment_box\">";
			$comment_box_uri = drizzo_theme('directory') . "comment_box.php?post_id=" . $post_id;
			$comments_string .= file_get_contents($comment_box_uri);
			$comments_string .= "</div>\n";
		}
	}
	return $comments_string;	
}

function drizzo_comment_save() {
	global $drizzo_comments;
	$post_id = $_POST['post_id'];
	$date = time();
	$content = addslashes(htmlentities($_POST['content']));
	$title = addslashes($_POST['title']);
	$author_id = $_POST['author_id'];
	$author_name = addslashes($_POST['author_name']);
	$author_email = addslashes($_POST['author_email']);
	$author_url = addslashes($_POST['author_url']);
	$author_ip = addslashes($_POST['author_ip']);
	$comment_insert = mysql_query("INSERT INTO `$drizzo_comments` (`post_id`,`date`,`content`,`title`,`author_id`,`author_name`,`author_email`,`author_url`,`author_ip`) VALUES('$post_id','$date','$content','$title','$author_id','$author_name','$author_email','$author_url','$author_ip')");
	if($comment_insert) {
		return true;
	} else {
		return false;
	}
}

function drizzo_comment_count() {
	global $the_post, $drizzo_comments;

	drizzo_db_connect();

	$comment_count = 0;

	if(!$the_post) {
		return false;
	} elseif($the_post['comment_count']) {
		$comment_count = $the_post['comment_count'];
	} elseif($the_post['ID']) {
		$comments_query = @mysql_query("SELECT `post_id` FROM `$drizzo_comments` WHERE `post_id`='$the_post[ID]' AND `status`='approved'");
		if($comments_query) {
			$comment_count = @mysql_num_rows($comments_query);				
		} else {
			$comment_count = 0;
		}
	} else {
		$comment_count = false;
	}
	return $comment_count;
}

function drizzo_comment_status() {
	global $the_post;
	
	$comment_status = "";
	
	if(!$the_post) {
		return false;
	} else {
		$comment_status = $the_post['comment_status'];
		return $comment_status;
	}
}

function drizzo_comments_count($status) {
	global $drizzo_comments;
	if($status == 'all') {
		$comment_rows = @mysql_query("SELECT `id` FROM `$drizzo_comments`");
		if($comment_rows) {
			$count = @mysql_num_rows($comment_rows);
		} else {
			$count = 0;
		}
	} else {
		$comment_rows = @mysql_query("SELECT `id` FROM `$drizzo_comments` WHERE `status` LIKE '%$status%'");
		if($comment_rows) {
			$count = @mysql_num_rows($comment_rows);
		} else {
			$count = 0;
		}
	}
	return $count;
}

function drizzo_comment_post_info($post_id = null) {
	global $drizzo_posts;
	if($post_id) {
		$post_info_lookup = @mysql_query("SELECT `title`,`permalink_name`,`type` FROM `$drizzo_posts` WHERE `ID`='$post_id'");
		if($post_info_lookup) {
			$post_info = mysql_fetch_array($post_info_lookup);
			if($post_info['type'] == 'page') {
				$post_info['permalink'] = "/" . $post_info['permalink_name'];
			} elseif($post_info['type'] == 'image') {
				$post_info['permalink'] = drizzo_gallery_image_permalink($post_id);
			} else {
				$post_info['permalink'] = "/" . $post_info['type'] . "/" . $post_info['permalink_name'];
			}
		} else {
			$post_info = false;
		}
	} else {
		$post_info = false;
	}
	return $post_info;
}

function drizzo_comment_list($status = 'all', $time_start = false, $time_end = false, $limit = false, $offset = false, $sort = false, $direction) {
	global $drizzo_comments;
	$status = str_replace('view_', '', $status);
	if($status == 'recent' && !$time_start) {
		$time_start = time()-7*24*60*60;
	}
	if($status == 'recent' && !$time_end) {
		$time_end = time();
	}
	if($status == 'all') {
		$status = "'moderated','spam','approved'";
	} elseif($status == 'recent') {
		$status = "'moderated','approved'";
	} else {
		$status = "'" . $status . "'";
	}
	if(!$sort) {
		$sort = "ID";
	}
		$sql = "SELECT `ID`,`post_id`,`parent_id`,`date`,`author_name`,`author_email`,`status`,`title` FROM `$drizzo_comments` WHERE `status` IN ($status)";
		if($time_start && $time_end) {
			$sql .= " AND `date`>='" . $time_start . "' AND `date`<='" . $time_end . "'";
		} elseif(!$time_start && $time_end) {
			$sql .= " AND `date`<='" . $time_end . "'";
		} elseif($time_start && !$time_end) {
			$sql .= " AND `date`>='" . $time_start . "'";
		} else {
			// Do Nothing
		}
		$sql .= " ORDER BY `". $sort . "` " . $direction;
		if($offset && $limit) {
			$sql .= " LIMIT " . $offset . "," . $limit;
		}
//		echo "<pre>" . wordwrap($sql, 150) . "</pre>";
		$comment_lookup = @mysql_query($sql);
		if($comment_lookup) {
			while($row = @mysql_fetch_array($comment_lookup)) {
				$post_info = drizzo_comment_post_info($row['post_id']);
				$row['post_title'] = $post_info['title'];
				$row['post_permalink'] = $post_info['permalink'];
				if($row['title'] == '') {
					$row['title'] == '(Untitled)';
				}
				$comments[] = $row;
			}
		} else {
			$comments = false;
		}
		return $comments;
}

function drizzo_comment_table($status = 'all', $time_start = false, $time_end = false, $limit = false, $offset = false, $sort = false) {
	global $drizzo_admin_images;
	$string = null;
	$current_url = $_SERVER['SCRIPT_NAME'] . "?";
	if($_GET['action']) { $current_url .= "action=" . $_GET['action'] . "&amp;"; }
	if($_GET['time']) { $current_url .= "time=" . $_GET['time'] . "&amp;"; }
	$direction = $_GET['direction'];
	$comments = drizzo_comment_list($status, $time_start, $time_end, $limit, $offset, $sort, $direction);
	$alt_count = 0;

	$url_args = $_SERVER['QUERY_STRING'];
	$url_args = explode('&', $url_args);
	if(count($url_args) > 0) {
		foreach($url_args as $url_arg) {
			$url_arg = explode("=", $url_arg);
			$key = $url_arg[0];
			$val = $url_arg[1];
			if($key != 'time' && $key != null) {
				$new_url_args .= "&amp;" . $key . "=" . $val;
			}
		}
	}
	$string .= "<form style=\"clear: none;float: right;\">";
	$string .= "<p class=\"right\"><label style=\"float: left;padding:0;\">Time Frame</label>";
	$string .= "<select name=\"timeframe\" id=\"timeframe\" onchange=\"location = this.options[this.selectedIndex].value;\">";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=all\""; if(!$_GET['time']) { $string .= " selected"; } $string .= ">All</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=today\""; if($_GET['time'] == 'today') { $string .= " selected"; } $string .= ">Today</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_week\""; if($_GET['time'] == 'past_week' || (!$_GET['time'] && !$_GET['action'])) { $string .= " selected"; } $string .= ">Past Week</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_2_weeks\""; if($_GET['time'] == 'past_2_weeks') { $string .= " selected"; } $string .= ">Past 2 Weeks</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=this_month\""; if($_GET['time'] == 'this_month') { $string .= " selected"; } $string .= ">This Month</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_3_months\""; if($_GET['time'] == 'past_3_months') { $string .= " selected"; } $string .= ">Past 3 Months</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_6_months\""; if($_GET['time'] == 'past_6_months') { $string .= " selected"; } $string .= ">Past 6 Months</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=past_12_months\""; if($_GET['time'] == 'past_12_months') { $string .= " selected"; } $string .= ">Past 12 Months</option>";
	$string .= "<option value=\"" . $_SERVER['PHP_SELF'] . "?" . $new_url_args . "&amp;time=this_year\""; if($_GET['time'] == 'this_year') { $string .= " selected"; } $string .= ">This Year</option>";
	$string .= "</select>";
	$string .= "</p>";
	$string .= "</form>\n";

	$string .= "<form name=\"batch\" id=\"batch\" action=\"\" method=\"post\">\n";
	$string .= "<table>\n";
	$string .= "\t<tr id=\"first\">\n";
	$string .= "\t\t<th class=\"center narrow " . drizzo_up_or_down('ID') . "\"><a href=\"" . $current_url . "sort=ID&amp;direction=" . drizzo_swap_sort_direction('ID') . "\" title=\"Sort by ID\">ID</a></th><th class=\"" . drizzo_up_or_down('title') . "\"><a href=\"" . $current_url . "sort=title&amp;direction=" . drizzo_swap_sort_direction('title') . "\" title=\"Sort by Title\">Title</a></th><th class=\"" . drizzo_up_or_down('post_id') . "\"><a href=\"" . $current_url . "sort=post_id&amp;direction=" . drizzo_swap_sort_direction('post_id') . "\" title=\"Sort by Post\">Post</a></th><th class=\"" . drizzo_up_or_down('date') . "\"><a href=\"" . $current_url . "sort=date&amp;direction=" . drizzo_swap_sort_direction('date') . "\" title=\"Sort by Date\">Date</a></th><th class=\"" . drizzo_up_or_down('author_name') . "\"><a href=\"" . $current_url . "sort=author_name&amp;direction=" . drizzo_swap_sort_direction('author_name') . "\" title=\"Sort by Author\">Author</th>";
	if($status == 'view_all' || $status == 'view_recent') {
		$string .= "<th class=\"center narrow\">Status</th>";
	}
	$string .= "<th class=\"center\" colspan=\"3\">Action</th>";
	$string .= "<th class=\"narrow\">Batch</th>\n";
	$string .= "\t</tr>\n";
	if($comments) {
		foreach($comments as $comment) {
			if ($alt_count%2) { $alt = "alt"; } else { $alt = ""; }
			$string .= "\t<tr class=\"" . $alt . "\">\n";
			$string .= "\t\t<td class=\"center\">" . $comment['ID'] . "</td><td>" . $comment['title'] . "</td><td><a href=\"" . $comment['post_permalink'] . "\" title=\"View " . $comment['post_title'] . "\">" . $comment['post_title'] . "</a></td><td>" . date('M d, Y \a\t g:i A', $comment['date']) . "</td><td><a href=\"mailto:" . $comment['author_email'] . "\" title=\"Email Comment Author\">" . $comment['author_name'] . "</a></td>";
			if($status == 'view_all' || $status == 'view_recent') {
				$string .= "<td class=\"center\"><img src=\"/drizzo/images/" . $comment['status'] . ".png\" alt=\"" . ucwords($comment['status']) . "\" title=\"" . ucwords($comment['status']) . "\" /></td>";
			}
			if($comment['status'] == 'approved') {
				$string .= "<td class=\"center\" width=\"1\"><a href=\"" . $comment['post_permalink'] . "#comment-" . $comment['ID'] . "\" title=\"View the Comment\"><img src=\"" . $drizzo_admin_images . "view.png\" alt=\"View This Comment\" /></a></td>";
			} else {
				$string .= "<td class=\"center\" width=\"1\"><a href=\"comments.php?view=" . $comment['ID'] . "\" title=\"View the Comment\"><img src=\"" . $drizzo_admin_images . "view.png\" alt=\"View This Comment\" /></a></td>";
			}
			$string .= "<td class=\"center\" width=\"1\"><a href=\"comments.php?edit=" . $comment['ID'] . "\"><img src=\"" . $drizzo_admin_images . "edit.png\" alt=\"Edit This Comment\" /></a></td><td class=\"center\" width=\"1\"><a href=\"comments.php?delete=" . $comment['ID'] . "\"><img src=\"" . $drizzo_admin_images . "delete.png\" alt=\"Delete This Comment\" /></a></td>";
			$string .= "<td class=\"center\"><input type=\"checkbox\" name=\"batch_id[]\" value=\"" . $comment['ID'] . "\" class=\"checkbox\" /></td>\n";
			
			$string .= "\t</tr>\n";
			$alt_count++;
		}
	} else {
		$string .= "\t<tr class=\"alt\">\n";
		$string .= "\t\t<td colspan=\"10\"><br /><p class=\"alert\">No comments could be found that match the search criteria.</p></td>\n";
		$string .= "\t</tr>\n";
	}
	$string .= "</table>\n";
	$string .= "<input type=\"hidden\" name=\"process\" id=\"process\" value=\"\" />\n";
	$string .= "<p class=\"right\">Legend: <img src=\"/drizzo/images/approved.png\" alt=\"Approved\" /> = Approved, <img src=\"/drizzo/images/moderated.png\" alt=\"Moderated\" /> = Moderated, <img src=\"/drizzo/images/spam.png\" alt=\"Spam\" /> = Spam &nbsp; &nbsp; <input type=\"button\" class=\"submit select\" onclick=\"SetAllCheckBoxes('batch', 'batch_id[]', '')\" value=\"Deselect All\" /> <input type=\"button\" class=\"submit select\" onclick=\"SetAllCheckBoxes('batch', 'batch_id[]', 'selected')\" value=\"Select All\" /><br />\n";
	$string .= "<input type=\"submit\" class=\"submit\" onclick=\"document.batch.process.value = 'spam'\" value=\"Mark as Spam\" /> <input type=\"submit\" class=\"submit\" onclick=\"document.batch.process.value = 'delete';return window.confirm('Are you sure you want to delete these messages?');\" value=\"Delete Comments\" /> <input type=\"submit\" class=\"submit\" onclick=\"document.batch.process.value = 'approve'\" value=\"Approve Comments\" /></p>";
	$string .= "</form>\n";

	return $string;
}

function drizzo_last_comment_date($post_id) {
	global $drizzo_comments;
	
	drizzo_db_connect();
	
	$lookup = @mysql_query("SELECT `date` FROM `$drizzo_comments` WHERE `post_id`='$post_id' ORDER BY `date` DESC LIMIT 1");
	if($lookup) {
		$last_date = mysql_fetch_array($lookup);
		$last_date = $last_date['date'];
		return $last_date;
	} else {
		return false;
	}
}
?>